﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Web;
using System.ServiceModel.Activation;
using Dominio;
using LogicaNegocios.Manager;

namespace UIWeb.WcfServices
{

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class VideoServices
    {
        [OperationContract]
        public UploadFile DoUpload(string filename, byte[] content, bool append)
		{
			string folder = Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Videos"));
			if (!System.IO.Directory.Exists(folder))
				System.IO.Directory.CreateDirectory(folder);

			FileMode m = FileMode.Create;
			if (append)
				m = FileMode.Append;

			using (FileStream fs = new FileStream(folder + @"\" + filename, m, FileAccess.Write))
			{
				fs.Write(content, 0, content.Length);
			}

            UploadFile file = new UploadFile { Name = filename, FileStoreUrl = "../Videos/" + filename };
			return file;
		}

        [OperationContract]
        public List<Video> CrearVideo(string filename)
        {
            List<Video> videos = null;
            Video video = new Video();
            video.Titulo = filename;
            video.FilePath = "/Videos/" + filename;
            VideoManager.GetManager().CrearVideo(video);

            videos = VideoManager.GetManager().GetVideos();

            return videos;
        }

        [OperationContract]
        public List<Evento> GetEventos()
        {
            List<Evento> eventos = null;
            eventos = EventoManager.GetManager().GetEventosSimple();           

            return eventos;
        }

        [OperationContract]
        public List<Video> GetVideosPorEvento(int idEvento)
        {
            List<Video> videos = null;

            videos = VideoManager.GetManager().GetVideosByEvento(idEvento);
            Uri uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
            string hostUri = uri.GetLeftPart(UriPartial.Authority);

            foreach (Video video in videos)
            {
                string miPath = video.FilePath;
                video.FilePath = hostUri + miPath;
            }

            return videos;
        }

        [OperationContract]
        public List<Video> GetVideos()
        {
            List<Video> videos = null;

            videos = VideoManager.GetManager().GetVideos();

            return videos;
        }

        [OperationContract]
        public void AsociarVideoEvento(int idVideo, int idEvento)
        {
            VideoManager.GetManager().AsociarVideoEvento(idVideo, idEvento);
        }

        [OperationContract]
        public void EliminarVideoEvento(int idVideo, int idEvento)
        {
            VideoManager.GetManager().EliminarVideoEvento(idVideo, idEvento);
        }
    }

    [DataContract]
    public class UploadFile
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string FileStoreUrl { get; set; }
    }
}
